function MultiplePollItem(obj)
{
  this.obj = obj;

  this.getVoteButton = function()
  {
    var name = this.obj['name'];

    var button = newElement("button", {className: 'voteButton'});
    if( this.obj.participants.indexOf( wave.getViewer().getId() ) >=0 ) {;
      button.title = 'Unvote this option';
      button.innerHTML = "-";
      $(button).bind('click', {name:name}, function(ev) { 
          removeVote(ev.data.name); 
          })
    } else {
      button.title = 'Add your vote';
      button.innerHTML = "+";
      $(button).bind('click', {name:name}, function(ev) { 
          addVote(ev.data.name); 
          });
    }

    return button;
  }

  this.getNumVotes = function()
  {
    return this.obj.participants.length
  }

  this.getVoters = function()
  {
    return jQuery.map(this.obj.participants, function(id)
        { return wave.getParticipantById(id); });
  }
}

function renderPoll(state)
{
  renderTable(state, MultiplePollItem);
}

// TODO if the participant is already in the list, it is added again.
// currently relies on the check to only display this function when it's
// not present. I guess it's easy to do your own Javascript to post crap
// into the state whatever we do here, so it's not so important.
function addVote(option){
  var key = convertOptionToKey(option);
  var optionValue = getStateValue(key);

  if(optionValue!=null){
    optionValue.participants.push(wave.getViewer().getId());
    submitValue(key, optionValue);
  }
}

function removeVote(option){
  var key = convertOptionToKey(option);
  var optionValue = getStateValue(key);

  if(optionValue!=null){
  	var id = wave.getViewer().getId();

    optionValue.participants = jQuery.grep(optionValue.participants, function(i){ return (i != id); }); // Remove id from array
    submitValue(key, optionValue);
  }
}

